package com.wkbb.sports.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.sports.dto.TeachDocumentDto;
import com.wkbb.sports.dto.TeachPlanDto;
import com.wkbb.sports.feign.BasicFeignService;

import com.wkbb.sports.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.sports.mapper.HlifeTeachPlanDetailMapper;
import com.wkbb.sports.mapper.HlifeTeachPlanMapper;
import com.wkbb.sports.model.HlifeTeachPlan;
import com.wkbb.sports.model.HlifeTeachPlanDetail;
import com.wkbb.sports.service.TeachPlanService;
import com.wkbb.sports.utils.BeanBind;




@Service
@Slf4j
public class TeachPlanServiceImpl extends BaseServiceImpl<HlifeTeachPlan> implements TeachPlanService{
	
	@Autowired
	HlifeTeachPlanDetailMapper  hlifeTeachPlanDetailMapper;
	@Autowired
	HlifeTeachPlanMapper  hlifeTeachPlanMapper;
	
	@Autowired
	BasicFeignService  basicFeignService;
	@Autowired
	SettingService settingService;

	@Autowired
    public void setMapper(HlifeTeachPlanMapper trainMapper) {
        super.setMapper(trainMapper);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addTeachPlanForPad(HlifeTeachPlan hlifeTeachPlan) throws Exception {
		
		super.insert(hlifeTeachPlan);
		
		int firtCourse =hlifeTeachPlan.getFirstCourseCount();
		int secondCourse = hlifeTeachPlan.getSecondCourseCount();
		
		List<HlifeTeachPlanDetail> list = new ArrayList<>();
		
		for(int i=1;i<=firtCourse;i++){
			
			HlifeTeachPlanDetail  first = new HlifeTeachPlanDetail();
			first.setCourseSeq(i);
			first.setCourseTime(45);
			first.setPlanTerm("1");
			first.setStatus((byte)1);
			first.setTeachingPlanId(hlifeTeachPlan.getId());
			
			list.add(first);
			
		}
		
		for(int j=1;j<=secondCourse;j++){
			
			HlifeTeachPlanDetail  first = new HlifeTeachPlanDetail();
			first.setCourseSeq(j);
			first.setCourseTime(45);
			first.setPlanTerm("2");
			first.setStatus((byte)1);
			first.setTeachingPlanId(hlifeTeachPlan.getId());
			
			list.add(first);
			
		}
		
		hlifeTeachPlanDetailMapper.insertBatch(list);
		
	}

	@Override
	public int putExcelDoc(Long orgId,List<TeachDocumentDto> list) throws Exception{
		//先查学校有没自定义，没有再根据年级类型查
		List<GradeDto> gradeList = settingService.getOrgGradeList(null);
		if (!gradeList.isEmpty()){
//			log.info("gradeList:"+JsonUtil.stringify(gradeList));
			for (TeachDocumentDto item : list){
				String[] ids = item.getGradeIds().split(",");
				StringBuilder sb = new StringBuilder();
				StringBuilder sb1 = new StringBuilder();
				for(String id:ids){
					GradeDto grade = gradeFindIndex(gradeList,id);
					if (grade != null){
						sb.append(grade.getId()).append(",");
						sb1.append(grade.getGradeName()).append(",");
					}
				}
				if (sb.length() > 0){
					sb.deleteCharAt(sb.length() - 1);
					sb1.deleteCharAt(sb1.length() - 1);
				}
				item.setGradeIds(sb.toString());
				item.setGradeNames(sb1.toString());
			}
		}
		return hlifeTeachPlanDetailMapper.insertDocumentList(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int delTeachPlan(Long id) {
		int result = hlifeTeachPlanMapper.delTeachPlan(id);
		if (result > 0){
			return hlifeTeachPlanDetailMapper.delByPlanId(id);
		}
		return 0;
	}

	private GradeDto gradeFindIndex(List<GradeDto> gradeList,String id){
		for (int i = 0; i < gradeList.size(); i++) {
			GradeDto grade = gradeList.get(i);
			if (grade.getOrders().toString().equals(id)){
				return grade;
			}
		}
		return null;
	}

	@Override
	public PageInfo<TeachPlanDto> selTeachPlanList(Map<String, Object> params, int pageNo, int pageSize)
			throws Exception {
		
		Page<HlifeTeachPlan> page = PageHelper.startPage(pageNo, pageSize, true);
		List<HlifeTeachPlan> plans = super.getListByMapParams(params);
		List<TeachPlanDto> dtos = new ArrayList<>();
		BeanBind bind = new BeanBind(HlifeTeachPlan.class, TeachPlanDto.class);
		for(HlifeTeachPlan plan : plans) {
			TeachPlanDto dto = (TeachPlanDto)bind.bindBean(plan);
			dto.setOrgName(basicFeignService.getOrgById(plan.getOrgId()).getData().getName());
			dto.setGradeName(basicFeignService.getGrade(plan.getGradeId()).getData().getGradeName());
			dtos.add(dto);
		}
        PageInfo<TeachPlanDto> pageInfo = new PageInfo<TeachPlanDto>(dtos);
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
		
	}

}
